package net.fltx.spai.controller;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.web.WebEngine;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class MainForm implements Initializable {
    @FXML
    private TabPane tabPane;
    @FXML
    private Tab add;

    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<Tab> tabs = tabPane.getTabs();
        try {
            Tab new_page = new Tab("New Page");
            URL url = getClass().getResource("/window.fxml");

            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(url);
            fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
            Parent root = fxmlLoader.load();
            new_page.setContent(root);
            Window controller = (Window)fxmlLoader.getController();
            tabs.add(tabs.size()-1, new_page);
            tabPane.getSelectionModel().select(new_page);
            WebEngine webEngine = controller.getWebEngine();
            webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
                new_page.setText(webEngine.getTitle());
            });

        } catch (IOException e) {
            e.printStackTrace();
        }


    }



}
